<?php
/**
 * Created by PhpStorm.
 * User: yuanbo
 * Date: 2018/8/5
 * Time: 下午10:04
 */

namespace app\admin\controller;
use think\Controller;

class ClubAdmin extends  Controller
{
//******俱乐部管理员列表*********
 public function clubAdminList()
 {
    $adminList = db('admin')->where('role_id',0)->join('club','admin.club_id=club.club_id','left')
        ->select();
    $this->assign('adminList',$adminList);
    return $this->fetch('ClubAdmin/clubAdminList');
 }

 //******修改俱乐部管理员
    public  function  updateAdmin()
    {
        //管理员id
        $admin_id = input('admin_id');
        //管理员信息
        $admin = db('admin')->where('admin_id',$admin_id)->join('club','admin.club_id=club.club_id','left')->find();
        //俱乐部列表
        $clubList = db('club')->where('admin','<',1)->select();
        $this->assign('admin',$admin);
        $this->assign('clubList',$clubList);
        return $this->fetch('ClubAdmin/updateAdmin');
    }
    //修改
    public function update()
    {
        //俱乐部管理员id
        $admin_id = input('admin_id');
        //管理员名
        $admin_name = input('admin_name');
        //俱乐部id
        $club_id = input('club_id');
        //修改的信息
        $date = ['admin_name'=>$admin_name,'club_id'=>$club_id];
        $clubdate = ['admin'=>1];
        $adminrs = db('admin')->where('admin_id',$admin_id)->update($date);
        $rs = db('club')->where('club_id',$club_id)->update($clubdate);
        if ($rs||$adminrs)
        {

            $this->success('修改成功','ClubAdmin/clubAdminList');
        }
        else
        {
            $this->success('修改失败');
        }

    }
    //**********添加俱乐部管理员*********
    public function addAdmin()
    {

        return $this->fetch('ClubAdmin/addAdmin');
    }
    //添加
    public  function  add()
    {
        //俱乐部管理员名
        $admin_name = input('admin_name');
        //俱乐部id
        $club_id = 0;
        //俱乐部管理员密码，初始化为11
        $admin_password = md5('11');
        //角色id
        $role_id = 0;
        //创建时间
        $create_time = date('Y:m:d h:i:s');

        //添加的信息
        $data = ['admin_id'=>null,'admin_name'=>$admin_name,'admin_password'=>$admin_password,'club_id'=>$club_id,'role_id'=>$role_id,
            'create_time'=>$create_time];
        $addRs = db('admin')->insert($data);


        if ($addRs)
        {
         $this->success('添加俱乐部管理员成功');
        }
        else
        {
            $this->error('添加俱乐部管理员失败');
        }
    }
    //***********删除俱乐部管理员***********
    public function  delete()
    {
        $admin_id = input('admin_id');
        $club_id = db('admin')->where('admin_id',$admin_id)->value('club_id');
        db('club')->where('club_id',$club_id)->update(['admin'=>0]);
        $rs = db('club')->where('club_id',$club_id)->delete();
        $json = [];
        if ($rs)
        {
            $json = ['success'=>1];
        }
        else
        {
            $json = ['success'=>0];
        }
        return json($json);
    }



}